package dk.pun.dums.client.widget;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DatePicker;

import dk.pun.dums.client.view.ViewCharacterView.Presenter;
import dk.pun.dums.shared.character.CharacterData;
import dk.pun.dums.shared.character.ExperiencePoints;

public class AddExperienceDialog extends DialogBox {

	private static CharacterRenameDialogUiBinder uiBinder = GWT.create(CharacterRenameDialogUiBinder.class);

	interface CharacterRenameDialogUiBinder extends UiBinder<Widget, AddExperienceDialog> {}

	@UiField protected transient DatePicker datePicker;
	@UiField protected transient CheckBox impressiveGearCheckBox;
	@UiField protected transient ListBox impressiveGearListBox;
	@UiField protected transient CheckBox goodRoleplayCheckBox;
	@UiField protected transient ListBox goodRoleplayListBox;
	@UiField protected transient CheckBox plotEngagementCheckBox;
	@UiField protected transient ListBox plotEngagementListBox;
	@UiField protected transient CheckBox extraInitiativeCheckBox;
	@UiField protected transient ListBox extraInitiativeListBox;
	@UiField protected transient CheckBox carrotsCheckBox;
	@UiField protected transient ListBox carrotsListBox;
	@UiField protected transient TextArea descriptionTextArea;
	
	private final transient Presenter presenter;
	private final transient CharacterData characterData;

	public AddExperienceDialog(final Presenter presenter, final CharacterData characterData) {
		super();
		this.setWidget(uiBinder.createAndBindUi(this));
		this.setText("Tilføj erfaringspoint");
		this.setAutoHideEnabled(true);
		this.setGlassEnabled(true);
		
		this.presenter = presenter;
		this.characterData = characterData;
		
		this.datePicker.setValue(new Date());
	}

	@UiHandler("okButton")
	public void okButtonClicked(final ClickEvent event) {
		this.presenter.onAddExperience(this.characterData, new ExperiencePoints(this.datePicker.getValue(), this.getPoints(), this.descriptionTextArea.getValue()));
		this.hide();
	}
	
	@UiHandler("cancelButton")
	public void cancelButtonClicked(final ClickEvent event) {
		this.hide();
	}

	private int getPoints() {
		int result = 0;
		if (this.impressiveGearCheckBox.getValue()) {
			result += this.impressiveGearListBox.getSelectedIndex()+1;
		}
		if (this.goodRoleplayCheckBox.getValue()) {
			result += this.goodRoleplayListBox.getSelectedIndex()+1;
		}
		if (this.plotEngagementCheckBox.getValue()) {
			result += this.plotEngagementListBox.getSelectedIndex()+1;
		}
		if (this.extraInitiativeCheckBox.getValue()) {
			result += this.extraInitiativeListBox.getSelectedIndex()+1;
		}
		if (this.carrotsCheckBox.getValue()) {
			result += this.carrotsListBox.getSelectedIndex()+1;
		}
		return result;
	}
}
